#include <QPainter>

#include "UIButton.h"
#include "UIFont.h"

// ===========================================================================
//  PRIVATE Class
// ===========================================================================
class UIButtonPrivate {
    public:
        UIButtonType buttonType;
        QColor titleShadowColor;
        QBrush backgroundColor;
        qreal shadowOffset;
        QColor borderColor;
        QColor titleColor;
        QString title;
        QFont font;
};

// ===========================================================================
//  PUBLIC Constructors/Destructor
// ===========================================================================
UIButton::UIButton (QGraphicsItem *parent)
    : UIControl(parent), d(new UIButtonPrivate)
{
    d->buttonType = UIButtonTypeRect;
    d->shadowOffset = 0.0f;
    d->font = UIFontNormal(font());
    d->titleShadowColor = Qt::black;
    d->backgroundColor = Qt::white;
    d->borderColor = Qt::black;
    d->titleColor = Qt::black;
}

UIButton::UIButton (UIButtonType type, QGraphicsItem *parent)
    : UIControl(parent), d(new UIButtonPrivate)
{
    d->buttonType = type;
    d->shadowOffset = 0.0f;
    d->font = UIFontNormal(font());
    d->titleShadowColor = Qt::black;
    d->backgroundColor = Qt::white;
    d->borderColor = Qt::black;
    d->titleColor = Qt::black;
}

UIButton::~UIButton() {
    delete d;
}

// ===========================================================================
//  PUBLIC Properties
// ===========================================================================
QFont UIButton::font (void) const {
    return(d->font);
}

void UIButton::setFont (const QFont& font) {
    d->font = font;
}

QString UIButton::title (void) const {
    return(d->title);
}

void UIButton::setTitle (const QString& title) {
    d->title = title;
}

QColor UIButton::titleColor (void) const {
    return(d->titleColor);
}

void UIButton::setTitleColor (const QColor& color) {
    d->titleColor = color;
}

qreal UIButton::titleShadowOffset (void) const {
    return(d->shadowOffset);
}

void UIButton::setTitleShadowOffset (qreal offset) {
    d->shadowOffset = offset;
}

QColor UIButton::titleShadowColor (void) const {
    return(d->titleShadowColor);
}

void UIButton::setTitleShadowColor (const QColor& color) {
    d->titleShadowColor = color;
}

QColor UIButton::borderColor (void) const {
    return(d->borderColor);
}

void UIButton::setBorderColor (const QColor& borderColor) {
    d->borderColor = borderColor;
}

QBrush UIButton::backgroundColor (void) const {
    return(d->backgroundColor);
}

void UIButton::setBackgroundColor (const QBrush& color) {
    d->backgroundColor = color;
}

UIButtonType UIButton::buttonType (void) const {
    return(d->buttonType);
}

void UIButton::setButtonType (UIButtonType type) {
    d->buttonType = type;
}

// ===========================================================================
//  PROTECTED Methods
// ===========================================================================
void UIButton::drawRect (QPainter *painter, const QRect& rect) {
    painter->setPen(d->borderColor);
    painter->setBrush(d->backgroundColor);
    switch (d->buttonType) {
        case UIButtonTypeRect:
            painter->drawRect(rect);
            break;
        case UIButtonTypeRoundedRect:
            painter->drawRoundedRect(rect, 10, 10);
            break;
        default:
            break;
    }

    painter->setFont(d->font);
    //painter->setPen(d->titleShadowColor);
    //painter->drawText(rect.x() + d->shadowOffset, rect.y() + d->shadowOffset, 
    //                  rect.width(), rect.height(), Qt::AlignCenter, d->title);

    painter->setPen(d->titleColor);
    painter->drawText(rect.x(), rect.y(), 
                      rect.width(), rect.height(), Qt::AlignCenter, d->title);
}

